package firstPack;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.KeyListener;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.gui.ComponentListener;
import org.newdawn.slick.gui.TextField;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;

/**
 * Displays the game over message and if total score for the game
 * is high enough a key listener is activated and a name is taken
 * in to update the high scores list. Delete letter functionality
 * has been incorporated.
 * State 3
 * @author El Jefe
 *
 */
public class GameOverState extends BasicGameState{

	public static String highName;
	private boolean reveal;
	
	@Override
	public void init(GameContainer container, StateBasedGame game)
			throws SlickException {
		highName = "";
		reveal = false;
//		nameInput = new TextField(container, null, 500, 400, 20, 20);
//		nameInput.setMaxLength(8);
//		nameInput.setText("Name");
//		nameInput.setBorderColor(Color.red);
		
	}

	@Override
	public void render(GameContainer container, StateBasedGame game, Graphics disp)
			throws SlickException {
		String gameOverString = "Game over\n\n";
		gameOverString += "Score : " + GameState.totalScore;
		gameOverString += "\nPress space for main menu";
		disp.drawString(gameOverString, 400, 200);
//		nameInput.render(container, disp);
		if(reveal){
			disp.drawString("You got a high score!\nEnter you name : " + highName, 400, 300);
		}
		
	}

	@Override
	public void update(GameContainer container, StateBasedGame sbg, int delta)
			throws SlickException {
		Input input = container.getInput();
		
		if(GameState.totalScore > GameState.minHighScore) {
			GameState.minHighScore = GameState.totalScore;
			reveal = true;
			input.addKeyListener(new MyKeyListener());
		}
		
		if(input.isKeyPressed(input.KEY_SPACE) || input.isKeyPressed(input.KEY_ENTER)){
			if(highName != ""){
				GameState.highScores.put(GameState.totalScore, highName.trim());
				GameState.updateMinScore();
				if(GameState.highScores.size() > 10){
					GameState.highScores.remove(GameState.minHighScore);
				}
				GameState.updateMinScore();
			}
			highName = "";
			reveal = false;
			input.removeAllKeyListeners();
			input.clearKeyPressedRecord();
			sbg.enterState(0, new FadeOutTransition(), new FadeInTransition());
		}
		
	}

	@Override
	public int getID() {
		// TODO Auto-generated method stub
		return 3;
	}
	

}

class MyKeyListener implements KeyListener{
	
	private boolean acceptInput = true;

	@Override
	public void inputEnded() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void inputStarted() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean isAcceptingInput() {
		if(GameOverState.highName.length()>8){
			acceptInput = false;
		}
		return acceptInput;
	}

	@Override
	public void setInput(Input arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyPressed(int key, char character) {
//		System.out.println(key + " " + character);
		
		
		
	}

	@Override
	public void keyReleased(int key, char character) {
		System.out.println(key + " " + character);
		if(character == ' '){
			acceptInput = false;
		}
		if(character =='\n'){
			acceptInput = false;
		}
		if(key == 14 && GameOverState.highName.length() > 0){
			GameOverState.highName = GameOverState.highName.substring(0, GameOverState.highName.length() - 1);
		}
		else if(acceptInput){
			GameOverState.highName += character;
		}
		
	}
	
}
